package com.cxyd.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.exception.RootException;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.modules.sys.dto.SysUserDTO;
import com.cxyd.modules.sys.service.SysUserService;
import com.cxyd.modules.wms.dao.*;
import com.cxyd.modules.wms.dto.ContainervwDTO;
import com.cxyd.modules.wms.dto.OrderReelDTO;
import com.cxyd.modules.wms.entity.*;
import com.cxyd.modules.wms.service.ContainervwService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;

/**
 * 容器信息
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@Service
public class ContainervwServiceImpl extends CrudServiceImpl<ContainervwDao, ContainervwEntity, ContainervwDTO> implements ContainervwService {
    private final SysUserService userService;


    public ContainervwServiceImpl(SysUserService userService) {
        this.userService = userService;

    }

    @Override
    public PageData<ContainervwDTO> page(Map<String, Object> params) {
        //1.查询容器信息
        final IPage<ContainervwEntity> page = baseDao.selectPage(
                getPage(params, "cont_id", true),
                getWrapper(params)
        );
        //2.获取用户信息
        final List<SysUserDTO> userList = userService.getList();
        //3.将creator与updater的username通过id进行绑定
        page.getRecords().forEach(entity -> {
            if (entity.getCreator() != null) {
                entity.setCreatorName(userService.getUsernameById(entity.getCreator(), userList));
            }
            if (entity.getUpdater() != null) {
                entity.setUpdaterName(userService.getUsernameById(entity.getUpdater(), userList));
            }
//

        });
        return getPageData(page, ContainervwDTO.class);
    }

    @Override
    public QueryWrapper<ContainervwEntity> getWrapper(Map<String, Object> params) {
        String contNo = (String) params.get("contNo");
        String contType = (String) params.get("contType");
        Integer isFull =
        ObjectUtils.isNotEmpty(params.get("isFull"))?Integer.parseInt((String) params.get("isFull")):null;

        QueryWrapper<ContainervwEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        wrapper.eq(StringUtils.isNotBlank(contType), "cont_type", contType);
        wrapper.eq(ObjectUtils.isNotEmpty(isFull), "is_full", isFull);

        return wrapper;
    }

    @Override
    public List<ContainervwDTO> getList(Map<String, Object> params) {
        //1.查询库存信息
        final List<ContainervwEntity> page = baseDao.selectList(
                getWrapper(params).orderByAsc("cont_id")
        );
        //2.获取用户列表
        final List<SysUserDTO> userList = userService.getList();
        //3.将creator与updater的username通过id进行绑定
        page.forEach(entity -> {
            if (entity.getCreator() != null) {
                entity.setCreatorName(userService.getUsernameById(entity.getCreator(), userList));
            }
            if (entity.getUpdater() != null) {
                entity.setUpdaterName(userService.getUsernameById(entity.getUpdater(), userList));
            }
        });
        return ConvertUtils.sourceToTarget(page, ContainervwDTO.class);
    }

}